package com.example.demo.arithmetic.datastructure.linkedlist2;

import java.util.Iterator;
import java.util.function.Consumer;

public class SingleLinkedListSentinel implements Iterable<Integer> {
    // 哨兵节点
    private Node head = new Node(888, null);

    // 头插法
    public void addFirst(int element) {
        // 链表为空
        //head = new Node(element, null);
        // 链表非空
        head = new Node(element, head);
    }

    public void addLast(int element) {
        Node last = findLast();
        // 不会为null了 至少有一个哨兵节点
        //        if (last == null) {
        //            addFirst(element);
        //            return;
        //        }
        last.next = new Node(element, null);
    }

    public void loop1(Consumer<Integer> consumer) {
        Node p = head.next;
        while (p != null) {
            consumer.accept(p.val);
            p = p.next;
        }
    }

    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head.next; p != null; p = p.next) {
            consumer.accept(p.val);
        }
    }

    private Node findNode(int index) {
        int i = -1;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new IllegalArgumentException(String.format("index[%s]不合法", index));
        }
        return node.val;
    }


    public void insert(int index, int value) {
        //        if (index == 0) {
        //            addFirst(value);
        //        }


        Node prev = findNode(index - 1);

        if (prev == null) {
            throw new IllegalArgumentException(String.format("index[%s]不合法", index));
        }
        prev.next = new Node(value, prev.next);
    }


    public void removeFirst() {
        remove(0);
    }

    public void remove(int index) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw new IllegalArgumentException(String.format("index[%s]不合法", index));
        }
        // 被删除的节点
        Node removed = prev.next;

        if (removed == null) {
            throw new IllegalArgumentException(String.format("index[%s]不合法", index));
        }
        prev.next = removed.next;
    }


    /**
     * 寻找最后一个元素
     *
     * @return
     */
    private Node findLast() {
        //        if (head == null) {
        //            return null;
        //        }
        Node p;

        for (p = head; p.next != null; p = p.next) {

        }
        return p;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;

            @Override
            public boolean hasNext() {
                return p != null;
            }

            @Override
            public Integer next() {
                Integer val = p.val;
                p = p.next;
                return val;
            }
        };
    }


    private static class Node {
        Node next;

        int val;

        public Node(int val, Node next) {
            this.next = next;
            this.val = val;
        }


    }

}



